package com.javaEE.gamepigeon.service.impl;

import com.javaEE.gamepigeon.dao.CompetitionDao;
import com.javaEE.gamepigeon.entity.Competition;
import com.javaEE.gamepigeon.entity.CompetitionBrief;
import com.javaEE.gamepigeon.entity.User;
import com.javaEE.gamepigeon.service.CompetitionService;
import com.javaEE.gamepigeon.util.FastDFSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;


@Service
public class CompetitionServiceImpl implements CompetitionService {

    @Autowired
    CompetitionDao competitionDao;

    @Autowired
    FastDFSUtils fastDFSUtils;

    @Value("${group}")
    String group;

    /**
     * 添加新的竞赛
     * @param publisherId 发布者的id
     * @param competitionName 竞赛的名字
     * @param nickName 竞赛的昵称
     * @param officialUrl 官方的url
     * @param infoDetail 详细信息
     * @param registerDetail 报名信息
     * @param imgFile 竞赛页面图片
     * @param titleImg 竞赛头像
     * @return 是否加入成功的bool值
     */
    @Override
    public boolean addCompetition(Integer publisherId, String competitionName, String nickName, String officialUrl, String infoDetail, String registerDetail, MultipartFile imgFile,MultipartFile titleImg) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String formatTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime);
        String imgUrl = null;
        String titleImgUrl = null;
        if(imgFile!=null){
            String[] filePath = FastDFSUtils.upLoad(imgFile);
            imgUrl = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+ filePath[1];
        }
        if(titleImg!=null){
            String[] filePath = FastDFSUtils.upLoad(titleImg);
            titleImgUrl = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+ filePath[1];
        }
        return competitionDao.addCompetition(publisherId, competitionName, nickName, officialUrl, infoDetail, registerDetail, imgUrl,titleImgUrl,formatTime) == 1;
    }

    /**
     * 更新赛事信息
     * @param competitionId 竞赛的Id
     * @param publisherId 发布者的Id
     * @param competitionName 竞赛名
     * @param nickName 昵称
     * @param officialUrl 官方的url
     * @param infoDetail 详细信息
     * @param registerDetail 报名信息
     * @param imgFile 竞赛页面图片
     * @param titleImg 竞赛头像
     * @return
     */
    @Override
    public boolean updateCompetition(Integer competitionId, Integer publisherId, String competitionName, String nickName, String officialUrl, String infoDetail, String registerDetail, MultipartFile imgFile, MultipartFile titleImg) {
        Competition competition = queryCompetition(competitionId,null,null,null,1,1).get(0);
        if(competition==null){
            return false;
        }
        String imgUrl=null,titleImgUrl=null,updateDate;
        updateDate = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        if(imgFile!=null){
            String[] filePath = FastDFSUtils.upLoad(imgFile);
            imgUrl = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+ filePath[1];
        }
        if(imgFile!=null&&competition.getTitleImg()!=null){
            fastDFSUtils.deleteFile(group,competition.getTitleImg().substring(33));
        }
        if(titleImg!=null){
            String[] filePath = FastDFSUtils.upLoad(titleImg);
            titleImgUrl = FastDFSUtils.getTrackerUrl()+filePath[0]+"/"+ filePath[1];
        }
        if(titleImg!=null&&competition.getImgUrl()!=null){
            fastDFSUtils.deleteFile(group,competition.getImgUrl().substring(33));
        }
        return competitionDao.updateCompetition(competitionId,publisherId,competitionName,nickName,officialUrl,infoDetail,registerDetail,imgUrl,titleImgUrl,updateDate) ==1;
    }

    /**
     * 加入tag
     * @param competitionId 比赛id
     * @param departmentId 专业id
     * @return 是否操作成功
     */
    @Override
    public boolean insertTag(Integer competitionId, Integer departmentId) {
        return competitionDao.insertTag(competitionId,departmentId)==1;
    }

    /**
     * 删除tag
     * @param tagId 标签的id
     * @return 是否操作成功
     */
    @Override
    public boolean deleteTag(Integer tagId) {
        return competitionDao.deleteTag(tagId) ==1;
    }

    /**
     * 搜索比赛
     * @param comId 比赛id
     * @param competitionName 竞赛名
     * @param nickName 竞赛昵称
     * @param departmentName 部门名
     * @param pageNum 页面数
     * @param pageSize 页面大小
     * @return
     */
    @Override
    public List<Competition> queryCompetition(Integer comId, String competitionName, String nickName, String departmentName, Integer pageNum, Integer pageSize) {
        return competitionDao.queryCompetition(comId,competitionName,nickName,departmentName,(pageNum-1)*pageSize,pageSize);
    }

    /**
     * 根据比赛ID删除比赛
     * @param comId 比赛的ID
     * @return 是否成功删除比赛
     */
    @Override
    public boolean deleteCompetition(Integer comId) {
        Competition competition = queryCompetition(comId,null,null,null,1,1).get(0);
        if(competition==null){
            return false;
        }
        if(competition.getImgUrl()!=null){
            fastDFSUtils.deleteFile(group,competition.getImgUrl().substring(33));
        }
        if(competition.getTitleImg()!=null){
            fastDFSUtils.deleteFile(group,competition.getTitleImg().substring(33));
        }
        return competitionDao.deleteCompetition(comId) == 1;
    }

    @Override
    public List<CompetitionBrief> queryBriefCompetition(Integer comId, String competitionName, String nickName, String departmentName, Integer pageNum, Integer pageSize) {
        List<CompetitionBrief> list = competitionDao.queryBriefCompetition(comId,competitionName,nickName,departmentName,(pageNum-1)*pageSize,pageSize);
        Integer[] integers = competitionDao.queryUserFavorite(((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId());
        for (CompetitionBrief competitionBrief : list) {
            for (Integer integer : integers) {
                if (competitionBrief.getComId() == integer) {
                    competitionBrief.setInAttention(true);
                }
            }
        }
        return list;
    }


}